package com.tree;

/**
 * @BelongsProject: DataStructures
 * @BelongsPackage: com.tree
 * @Author: 江岸
 * @CreateTime: 2021-03-09 23:32
 * @Description: 顺序存储二叉树
 */
public class ArrayBinaryTreeDemo {

    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        ArrayBinary test = new ArrayBinary(nums);
        test.preOrder(0);
    }
}
class ArrayBinary{
    public int[] arr;

    public ArrayBinary(int[] arr) {
        this.arr = arr;
    }

    public void preOrder(int index){
        if (arr==null || arr.length==0){
            System.out.println("数组为空");
            return;
        }
        System.out.println(arr[index]);
        //左子树
        if (2*index+1<arr.length){
            preOrder(2*index+1);
        }
        //右子树
        if (2*index+2<arr.length){
            preOrder(2*index+2);
        }
    }
}
